package com.设计模式.strategy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @Date 2021/1/8 14:13
 * @Author admin
 * @Description 策略应用类
 */
@Service
public class MatchStrategyPattern {

    @Autowired
    private List<IMatchService> matchServices;

    public void match(Integer channelId, Map<String,Object> map){
        //根据参数channelId,从所有实现类中匹配具体的策略实现类
        Optional<IMatchService> optional = matchServices.stream().
                filter(service -> service.getChannelId().contains(channelId))
                .findFirst();
        /**
         * ifPresent 如果optional不为空则执行，反之不执行
         */
        optional.ifPresent(op -> {optional.get().match(map);});

    }

    public void add(){
        matchServices = new ArrayList<>();
        matchServices.add(new Match360ServiceImpl());
        matchServices.add(new MatchBaiDuServiceImpl());
    }

    public static void main(String[] args) {
        MatchStrategyPattern matchStrategyPattern = new MatchStrategyPattern();
        matchStrategyPattern.add();
        matchStrategyPattern.match(3,null);
    }
}
